<template>
  <BasicDrawer v-bind="$attrs" :showFooter="true" :okAuth="'m8:machine:edit'" @register="registerDrawer"
    @ok="handleSubmit" width="40%">
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8MachineMachineBarcodeForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { notifyFirstCheck } from '/@/api/m8/machine/machine';
  import { dateUtil } from '/@/utils/dateUtil';


  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('order.m8OperCheck');
  const { showMessage } = useMessage();
  const record = ref < any > ({});
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: '首检提示',
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('机床'),
      field: 'machineName',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('图名'),
      field: 'picname',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('图号'),
      field: 'picno',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('负责人'),
      field: 'user.userName',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('预约时间'),
      field: 'machine.prefirstCheckDate',
      defaultValue: dateUtil(new Date()),
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      required: true,
    },
   
  ];
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    // console.log(data);
    await resetFields();
    record.value = {
      picId: data.orderPic.id,
      picno: data.orderPic.picno,
      picname: data.orderPic.picname,
      "user.userCode": data.user.userCode,
      "user.userName": data.user.userName,
      machineId: data.machineId,
      machineName: data.machine.name,
      'task.taskCode': data.task.taskCode
    }
    record.value.__t = new Date().getTime();

    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        ...record.value,
        'machine.prefirstCheckDate': data.machine.prefirstCheckDate,
      };
      const res = await notifyFirstCheck(params);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>